package com.collectplus.express.order;

import java.io.Serializable;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.DialogMgr;

/**
 * 取消订单
 * 
 * @author coffee <br>
 *         2016-1-18下午3:47:54
 */
@SuppressLint("InflateParams")
public class OrderCancelPopwin {

	private final int optionSize = 6;

	private View[] items; // item项
	private EditText mContent;// 其他原因
	/**
	 * 可以传入int和String类型
	 */
	private Serializable orderId;

	public OrderCancelPopwin(Serializable orderId) {
		this.orderId = orderId;
	}

	private void setButtonStyle(BaseActivity2 activity, View layout, Button submit) {
		boolean hasSelect = false;
		for (int i = 1; i <= optionSize; i++) {
			if (items[i - 1].isSelected()) {
				hasSelect = true;
				break;
			}
		}
		// TODO 现在不填原因也允许取消
		hasSelect = true; // 强制设为true
		activity.setButtonEnableStyle(submit, hasSelect);
		if (hasSelect) {
			submit.setEnabled(true);
		} else {
			submit.setEnabled(false);
		}
	}

	/**
	 * 获取选中的ids
	 * 
	 * @return
	 */
	private String getSelectIds() {
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= optionSize; i++) {
			if (items[i - 1].isSelected()) {
				sb.append(i).append(",");
			}
		}
		return sb.toString();
	}

	public PopupWindow show(Activity context) {
		items = new View[6];
		final BaseActivity2 activity = (BaseActivity2) context;
		final View layout = LayoutInflater.from(context).inflate(R.layout.order_cancel, null);
		final PopupWindow popwin = DialogMgr.createPopupWindow(context, layout);
		View anchor = context.findViewById(R.id.app_title_layout);
		Rect rect = DialogMgr.locateView(anchor);
		popwin.setAnimationStyle(R.style.popup_window_top2bottom);
		popwin.update();
		popwin.showAtLocation(anchor, Gravity.TOP | Gravity.LEFT, rect.left, rect.top);
		//
		layout.findViewById(R.id.title_right).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popwin.dismiss();
			}
		});
		final Button submit = (Button) layout.findViewById(R.id.order_cancel_submit);
		mContent = (EditText) layout.findViewById(R.id.order_item_content);
		// TODO 现在不填原因也允许取消 -- 以后如果需要改需求的话 、以下两行代码需要改
		// activity.setButtonEnableStyle(submit, false);
		// submit.setEnabled(false);//
		for (int i = 1; i <= optionSize; i++) {
			int id = context.getResources().getIdentifier("order_item_" + i, "id", context.getPackageName());
			items[i - 1] = layout.findViewById(id);
			items[i - 1].setTag(i);
			items[i - 1].setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					v.setSelected(!v.isSelected());
					setButtonStyle(activity, layout, submit);
				}
			});
		}

		submit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AppHttp.getInstance().orderCancel(orderId,null,0,null);
				popwin.dismiss();
			}
		});
		return popwin;
	}
}
